import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Index.vue'
import vuex from '../store/index';

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home
  },
  {
    path: '/UserManage',
    name: 'UserManage',
    component: () => import('../components/UserManage.vue'),
  },
  {
    path: '/CustomerManage',
    name: 'CustomerManage',
    component: () => import('../components/CustomerManage.vue'),
  },
  {
    path: '/OrderManage',
    name: 'OrderManage',
    component: () => import('../components/OrderManage.vue'),
  },
  {
    path: '/LoginForm',
    name: 'LoginForm',
    component: () => import('../components/LoginForm.vue'),
  },
]

const router = new VueRouter({
  routes
})

//路由守卫
router.beforeEach((to, from, next) => {
  // 首页放行
  if (to.path === '/') return next();

  // 未登录拦截
  if (vuex.state.user == "") {
    alert("请先在左下角点击登录")
    return next("/")
  }

  // 禁用用户拦截
  if (vuex.state.user.status == 0) {
    alert("用户已被禁用")
    return next("/")
  }

  // 非管理员拦截
  if(to.path ==='/UserManage'){
    if(vuex.state.user.type==0){
      alert("该账号无权限访问")
      return next("/")
    }
    return next()
  }

  
  return next()
})



export default router
